package com.warmlight.voicepacket.fragment.main;

import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.squareup.otto.Subscribe;
import com.umeng.analytics.MobclickAgent;
import com.warmlight.voicepacket.MApplication;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.adapter.main.HomeFragmentPagerAdapter;
import com.warmlight.voicepacket.base.fragment.BaseFragment;
import com.warmlight.voicepacket.common.OttoConstants;
import com.warmlight.voicepacket.db.SharedPerfenceConstant;
import com.warmlight.voicepacket.fragment.comm.PictureFragment;
import com.warmlight.voicepacket.fragment.comm.RecommFragment;
import com.warmlight.voicepacket.fragment.comm.VideoFragment;
import com.warmlight.voicepacket.ottoEvent.OPictureBean;
import com.warmlight.voicepacket.ottoEvent.ORecommBean;
import com.warmlight.voicepacket.ottoEvent.OVideoBean;
import com.warmlight.voicepacket.ottoEvent.OttoBus;
import com.warmlight.voicepacket.ottoEvent.CommRefreshBean;
import com.warmlight.voicepacket.plugin.QmzbMediaManager;
import com.warmlight.voicepacket.utils.MobClickHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by weiweijinpeng on 2018/9/7.
 */

public class CommunityFragment extends BaseFragment implements View.OnClickListener{


    @BindView(R.id.main_viewpager)
    ViewPager qmzbViewPager;

    @BindView(R.id.loading_progress)
    ProgressBar mLoadingBar;

    @BindView(R.id.comm_recomm)
    TextView mRecommView;

    @BindView(R.id.comm_video)
    TextView mVideoView;

    @BindView(R.id.comm_picture)
    TextView mPictureView;

    TextView selectTab;
    ScaleAnimation blowUpAnim;
    ScaleAnimation shrinkAnim;
    ScaleAnimation defaultAnim;

    RecommFragment recommFragment;
    VideoFragment videoFragment;
    PictureFragment pictureFragment;

    List<Fragment> list;
    HomeFragmentPagerAdapter adapter;

    int position=0;

    int fragmentType;

    boolean videoFist=true;//判断是否第一进入fragment页面，如果第二次直接播放上次滑走的位置视频

    @Override
    public void onStart() {
        super.onStart();
        //注册到bus事件总线中
        OttoBus.getInstance().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        OttoBus.getInstance().unregister(this);
    }

    // 接受事件
    @Subscribe
    public void setContent(CommRefreshBean data) {
        if (data.getStatus().equals(OttoConstants.COMMREFRESH)){
            startRefresh();
        }else{
            stopRefresh();
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home_slide;
    }

    @Override
    public void initViews() {

        slideAnim();
        qmzbViewPager.setOnPageChangeListener(new MyPagerChangeListener());
        recommFragment = new RecommFragment();
        videoFragment = new VideoFragment();
        pictureFragment = new PictureFragment();

        //把Fragment添加到List集合里面
        list = new ArrayList<>();
        list.add(recommFragment);
        list.add(videoFragment);
        list.add(pictureFragment);

        adapter = new HomeFragmentPagerAdapter(getChildFragmentManager(), list);
        qmzbViewPager.setAdapter(adapter);
        qmzbViewPager.setCurrentItem(0);
        qmzbViewPager.setOffscreenPageLimit(3);

        startScaleAnimation(mRecommView,0);

        mRecommView.setTextColor(getResources().getColor(R.color.black));
        mVideoView.setTextColor(getResources().getColor(R.color.text_color_1));
        mPictureView.setTextColor(getResources().getColor(R.color.text_color_1));

        mRecommView.setShadowLayer(1,0,0, MApplication.getInstance().getResources().getColor(R.color.white));
        mVideoView.setShadowLayer(1,0,0, MApplication.getInstance().getResources().getColor(R.color.white));
        mPictureView.setShadowLayer(1,0,0, MApplication.getInstance().getResources().getColor(R.color.white));
    }

    /**
     * 首页左右滑动tab动画
     */
    private void slideAnim(){
        blowUpAnim = new ScaleAnimation(
                1.0f, 1.3f, 1.0f, 1.3f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        blowUpAnim.setDuration(200);
        blowUpAnim.setFillAfter(true);

        shrinkAnim = new ScaleAnimation(
                1.3f, 1.0f, 1.3f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        shrinkAnim.setDuration(200);
        shrinkAnim.setFillAfter(true);

        defaultAnim = new ScaleAnimation(
                1.0f, 1.0f, 1.0f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        defaultAnim.setDuration(0);
        defaultAnim.setFillAfter(true);
    }
    @Override
    public void initData() {
    }
    @OnClick({R.id.comm_recomm, R.id.comm_video, R.id.comm_picture,R.id.loading_progress})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.comm_recomm:
                selectedTab(0);
                qmzbViewPager.setCurrentItem(0);
                break;
            case R.id.comm_video:
                selectedTab(1);
                qmzbViewPager.setCurrentItem(1);
                break;
            case R.id.comm_picture:
                selectedTab(2);
                qmzbViewPager.setCurrentItem(2);
                break;
            case R.id.loading_progress:
                if (fragmentType==0){
                    OttoBus.getInstance().post(new ORecommBean("",""));
                }else if (fragmentType==1){
                    OttoBus.getInstance().post(new OVideoBean("",""));
                }else if (fragmentType==2){
                    OttoBus.getInstance().post(new OPictureBean("",""));
                }
                break;
        }
    }


    /**
     * 设置一个ViewPager的侦听事件，当左右滑动ViewPager时菜单栏被选中状态跟着改变
     */
    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:
                    selectedTab(0);
                    MobclickAgent.onEvent(getContext(), MobClickHelper.pla_topbar_rec);
                    break;
                case 1:
                    selectedTab(1);
                    MobclickAgent.onEvent(getContext(),MobClickHelper.pla_topbar_video);
                    break;
                case 2:
                    selectedTab(2);
                    MobclickAgent.onEvent(getContext(),MobClickHelper.pla_topbar_pic);
                    break;
            }
        }
    }

    /**
     * 开始缩放动画
     */
    private void startScaleAnimation(TextView tabView,int type) {
        if (selectTab != tabView) {
            tabView.startAnimation(blowUpAnim);
        } else {
            return;
        }
        if (selectTab != null) {
            if (type>position && type==1){
                mRecommView.startAnimation(shrinkAnim);
                mPictureView.startAnimation(defaultAnim);
            }else if (type>position && type==2){
                mVideoView.startAnimation(shrinkAnim);
                mRecommView.startAnimation(defaultAnim);
            }else if (type<position && type==1){
                mPictureView.startAnimation(shrinkAnim);
                mRecommView.startAnimation(defaultAnim);
            }else if (type<position && type==0){
                mVideoView.startAnimation(shrinkAnim);
                mPictureView.startAnimation(defaultAnim);
            }
        }
        selectTab = tabView;
        position=type;
    }

    /**
     * 选择tab切换
     */
    public void selectedTab(int type){
        fragmentType=type;
        mSharePreferenceUtil.setInt(SharedPerfenceConstant.COMMTABTYPE,type);

        if (QmzbMediaManager.isPlaying())//这里有报过空指针QmzbMediaIjkPlayer.java:77
            QmzbMediaManager.pause();

        mRecommView.setTextColor(getResources().getColor(R.color.text_color_1));
        mVideoView.setTextColor(getResources().getColor(R.color.text_color_1));
        mPictureView.setTextColor(getResources().getColor(R.color.text_color_1));

        mRecommView.setShadowLayer(1,0,0,  MApplication.getInstance().getResources().getColor(R.color.white));
        mVideoView.setShadowLayer(1,0,0,  MApplication.getInstance().getResources().getColor(R.color.white));
        mPictureView.setShadowLayer(1,0,0,  MApplication.getInstance().getResources().getColor(R.color.white));
        if (type==0){

            OttoBus.getInstance().post(new ORecommBean(OttoConstants.COMMPLAY,""));

            mRecommView.setTextColor(getResources().getColor(R.color.black));
            startScaleAnimation(mRecommView,0);
        }else if (type==1){
            if (videoFist){

                videoFist=false;
            }else if (!videoFist){
                OttoBus.getInstance().post(new OVideoBean(OttoConstants.COMMPLAY,""));
            }
            mVideoView.setTextColor(getResources().getColor(R.color.black));
            startScaleAnimation(mVideoView,1);
        }else if (type==2){

            try {
                QmzbMediaManager.pause();
            }catch (Exception e){
                e.printStackTrace();
            }
            mPictureView.setTextColor(getResources().getColor(R.color.black));
            startScaleAnimation(mPictureView,2);
        }
    }

    /**
     * 开始刷新动画
     */
    public void startRefresh() {
        mLoadingBar.setIndeterminateDrawable(getResources().getDrawable(
                R.drawable.sample_footer_loading_progress));
        mLoadingBar.setProgressDrawable(getResources().getDrawable(
                R.drawable.sample_footer_loading_progress));
    }

    /**
     * 停止刷新动画
     */
    public void stopRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mLoadingBar.setIndeterminateDrawable(getResources().getDrawable(
                        R.drawable.refresh));
                mLoadingBar.setProgressDrawable(getResources().getDrawable(
                        R.drawable.refresh));
            }
        },500);
    }

}
